package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.ticket.basis.mapper.TicketWarehouseInfoMapper;
import cn.darkhorse.ticket.basis.pojo.TicketWarehouseInfo;
import cn.darkhorse.ticket.basis.service.TicketWarehouseInfoService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import cn.darkhorse.ticket.store.pojo.TicketOutboundSub;
import cn.darkhorse.ticket.store.pojo.TicketWarehousingSub;
import cn.darkhorse.ticket.store.service.TicketOutboundService;
import cn.darkhorse.ticket.store.service.TicketOutboundSubService;
import cn.darkhorse.ticket.store.service.TicketWarehousingSubService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 仓库信息 服务实现类
 * </p>
 *
 * @author maoguanjun
 * @since 2024-01-10
 */
@Service
public class TicketWarehouseInfoServiceImpl extends ServiceImpl<TicketWarehouseInfoMapper, TicketWarehouseInfo> implements TicketWarehouseInfoService {
    @Resource
    private RepeatCheck<TicketWarehouseInfo> repeatCheck;
    @Resource
    private TicketOutboundSubService ticketOutboundSubService;
    @Resource
    private TicketWarehousingSubService ticketWarehousingSubService;
    /**
     * 列表
     *
     * @param dataName
     * @return
     */
    @Override
    public List<TicketWarehouseInfo> listAll(String dataName) {
        LambdaQueryWrapper<TicketWarehouseInfo> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dataName)){
            wrapper.like(TicketWarehouseInfo::getName,dataName);
        }
        wrapper.orderByDesc(TicketWarehouseInfo::getGmtCreate);
        List<TicketWarehouseInfo> list = list(wrapper);
        return list;
    }

    /**
     * 保存
     *
     * @param info
     * @return
     */
    @Override
    public boolean saveData(TicketWarehouseInfo info) {
        //TODO 生成编码
        repeatCheck.repeat(this,info, TicketConstants.BUSINESS_TYPE_CK);
        LambdaQueryWrapper<TicketWarehouseInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TicketWarehouseInfo::getName,info.getName());
        if(null!=info.getId()){
            if(isQuote(info.getId())){
                throw new ServiceException("该仓库已被使用，不允许修改!");
            }
            wrapper.ne(TicketWarehouseInfo::getId,info.getId());
        }
        long count = count(wrapper);
        if(count>0){
            throw new ServiceException("该仓库名系统已存在！");
        }
        return saveOrUpdate(info);
    }

    /**
     * 删除
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean delete(Long dataId) {
        //TODO 删除校验
        if(isQuote(dataId)){
            throw new ServiceException("该仓库已被使用，不允许删除!");
        }
        return removeById(dataId);
    }

    /**
     * 查看
     *
     * @param dataId
     * @return
     */
    @Override
    public TicketWarehouseInfo view(Long dataId) {
        return getById(dataId);
    }

    /**
     * 是否被引用
     *
     * @param dataId
     * @return
     */
    @Override
    public boolean isQuote(Long dataId) {
        //出库
        LambdaQueryWrapper<TicketOutboundSub> outWrapper=new LambdaQueryWrapper<>();
        outWrapper.eq(TicketOutboundSub::getWarehouseInfoId,dataId);
        long outCount = ticketOutboundSubService.count(outWrapper);
        //入库
        LambdaQueryWrapper<TicketWarehousingSub> inWrapper=new LambdaQueryWrapper<>();
        inWrapper.eq(TicketWarehousingSub::getWarehouseInfoId,dataId);
        long inCount = ticketWarehousingSubService.count(inWrapper);
        if(outCount>0||inCount>0){
            return  true;
        }
        return false;
    }
}
